/*
 * Copyright (c) 2021 com.youkeyi. All rights reserved.
 *
 *  @author liuxiawang
 *  @location Shenzhen.China
 *  @date 4/8/21 5:22 PM
 */

package com.youkeyi.ddy.cloud.common.context;

import com.alibaba.fastjson.JSONArray;
import com.youkeyi.ddy.cloud.common.context.entity.SystemContextEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 存储/获取当前线程的用户信息工具类
 *
 * @author Administrator.
 * @date 2021/03/23.
 * @time 19:16
 **/
@Component
public class YoukeyiSystemContext {

    private static ThreadLocal<SystemContextEntity> userThreadLocal = new ThreadLocal<>();
    public YoukeyiSystemContext() {}

    public static SystemContextEntity getUser(){return userThreadLocal.get();}

    public static void setUser(SystemContextEntity user) {userThreadLocal.set(user);}

    public static void remove(){userThreadLocal.remove();}

    public static String getCorpPrimaryKey(){
        if(null==getUser()||StringUtils.isBlank(getUser().getCorpPrimaryKey())){
            return null;
        }
        return  getUser().getCorpPrimaryKey();
    }

    public static Integer getCorpType(){
        if(null==getUser()||StringUtils.isBlank(getUser().getCorpType())){
            return null;
        }
        return Integer.valueOf(getUser().getCorpType());
    }

    public static String getUserPrimaryKey() {
        if (null == getUser() || StringUtils.isBlank(getUser().getUserPrimaryKey())) {
            return null;
        }
        return getUser().getUserPrimaryKey();
    }

    public static String getUserName() {
        if (null == getUser() || StringUtils.isBlank(getUser().getUserName())) {
            return null;
        }
        return getUser().getUserName();
    }

    public static String getUserPhone() {
        if (null == getUser() || StringUtils.isBlank(getUser().getUserPhone())) {
            return null;
        }
        return getUser().getUserPhone();
    }

    public static String getUserAccount() {
        if (null == getUser() || StringUtils.isBlank(getUser().getAccount())) {
            return null;
        }
        return getUser().getAccount();
    }

    public static Integer getIsRoot(){
        if(null==getUser()||StringUtils.isBlank(getUser().getIsRoot())){
            return null;
        }
        return Integer.valueOf(getUser().getIsRoot());
    }

    public static String getTenantPrimaryKey() {
        if (null == getUser() || StringUtils.isBlank(getUser().getCorpPrimaryKey())) {
            return null;
        }
        return getUser().getCorpPrimaryKey();
    }

    public static String getEmployeePrimaryKey() {
        if (null == getUser() || StringUtils.isBlank(getUser().getEmployeePrimaryKey())) {
            return null;
        }
        return getUser().getEmployeePrimaryKey();
    }

    public static String getFromType(){
        if (null == getUser() || StringUtils.isBlank(getUser().getFromType())) {
            return null;
        }
        return getUser().getFromType();
    }

    public static String getPositionCode() {
        if (null == getUser() || StringUtils.isBlank(getUser().getPositionCode())) {
            return null;
        }
        return getUser().getPositionCode();
    }

    public static String getOrganizationKey() {
        if (null == getUser() || StringUtils.isBlank(getUser().getOrganizationKey())) {
            return null;
        }
        return getUser().getOrganizationKey();
    }
}
